<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <Header :addItem="addItem"></Header>
        <List :todos="todos" :removeItem="removeItem"></List>
        <Footer :todos="todos" :removeSelectItem="removeSelectItem" :selectAllItems="selectAllItems"></Footer>
      </div>
    </div>
  </div>
</template>

<script>
import './base.css'
import Header from './components/Header'
import List from './components/List'
import Footer from './components/Footer'

export default {
  name: 'App',
  components: { Header, List, Footer },
  props: {},
  data() {
    return {
      // 对象存储
      todos: JSON.parse(window.localStorage.getItem("todos_key") || '[]')
    }
  },
  methods: {
    addItem (item) {
      this.todos.unshift(item)
    },
    removeItem(index){
      this.todos.splice(index,1)
    },
    /*全选或者全不选*/
    selectAllItems(value){
      console.log(value)
      this.todos.forEach(todo=>todo.complete = value)
    },
    removeSelectItem(){
      this.todos = this.todos.filter(todo => !todo.complete)
    }
  },
  watch: {
    todos : {
      // 深度监视：对象属性改变
      deep : true,
      handler: function (value) {
        window.localStorage.setItem("todos_key",JSON.stringify(value))
      }
    }
  }
}
</script>

<style scoped>
.todo-container {
  width: 600px;
  margin: 0 auto;
}

.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>
